package cn.metafoo.banner.core;

import cn.hutool.core.thread.ThreadUtil;
import cn.metafoo.common.utils.monitor.IPUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;

import java.util.concurrent.TimeUnit;

/**
 * 项目启动成功后，提供文档相关的地址
 *
 * @Author: fatto
 * @create: 2023/3/23 17:05
 */
@Slf4j
@RequiredArgsConstructor
public class BannerApplicationRunner implements ApplicationRunner {
    private final Environment environment;

    @Override
    public void run(ApplicationArguments args) {
        ThreadUtil.execute(() -> {
            // 延迟 1 秒，保证输出到结尾
            ThreadUtil.sleep(1, TimeUnit.SECONDS);
            String serverPort = environment.getProperty("server.port", "8080");
            log.info("""
                            \n\t----------------------------------
                            \t项目 {} 启动成功！
                            \t服务地址: \thttp://localhost:{}\s
                            \t接口文档: \thttp://{}:{}/doc.html\s
                            \t----------------------------------""",
                    environment.getProperty("spring.application.name", ""),
                    serverPort, IPUtil.getServerInnerIp(), serverPort);
        });
    }
}
